package com.fx.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fx.commonuntils.ReturnMessage;
import com.fx.commonuntils.ReturnMessageUtils;
import com.fx.eduservice.entity.vo.EduCourse;
import com.fx.eduservice.entity.vo.EduCourseDescription;
import com.fx.eduservice.mapper.EduCourseMapper;
import com.fx.eduservice.request.CourseFrontVo;
import com.fx.eduservice.request.CourseInfoVo;
import com.fx.eduservice.response.curstom.CourseWebVo;
import com.fx.eduservice.response.curstom.CustrmResponse;
import com.fx.eduservice.service.EduCourseDescriptionService;
import com.fx.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-11-07
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {


    final private EduCourseDescriptionService eduCourseDescriptionService;
    final private EduCourseMapper eduCourseMapper;


    @Override
    public ReturnMessage<Object> addCourseInfo(CourseInfoVo courseInfoVo) {


        //new课程表对象
        EduCourse eduCourse = new EduCourse();
        //将请求对象中的值赋值到实体类中
        BeanUtils.copyProperties(courseInfoVo, eduCourse);


        //调用base中的insert方法
        baseMapper.insert(eduCourse);
        eduCourse.getId();


        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoVo.getDescription());


        //掉用课程简介service进行存储课程简介的信息
        eduCourseDescription.setId(eduCourse.getId());
        eduCourseDescriptionService.save(eduCourseDescription);


        return ReturnMessageUtils.success();
    }

    @Override
    public ReturnMessage<Object> getCourseInfoById(String id) {
        //根据课程id查询出对应的课程信息

        EduCourse eduCourse = this.getById(id);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse, courseInfoVo);


        //根据课程信息查询出对应的简介信息
        EduCourseDescription courseDescriptions = eduCourseDescriptionService.getById(id);
        courseInfoVo.setDescription(courseDescriptions.getDescription());


        //封装信息返回
        return ReturnMessageUtils.success(courseInfoVo);
    }

    @Override
    public ReturnMessage<Object> updataCourseInfo(CourseInfoVo courseInfoVo) {
        //修改课程信息
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, eduCourse);
        this.save(eduCourse);

        //修改简介信息
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setId(courseInfoVo.getId());
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        eduCourseDescriptionService.save(eduCourseDescription);


        return ReturnMessageUtils.success();
    }

    @Override
    public ReturnMessage<CustrmResponse> quaryCustormPublic(String id) {

        CustrmResponse custrmResponse = eduCourseMapper.quaryCustormPublic(id);


        return ReturnMessageUtils.success(custrmResponse);
    }

    @Override
    public Map<String, Object> getCourseFrontList(Page<EduCourse> pageParam, CourseFrontVo courseFrontVo) {
        //2 根据讲师id查询所讲课程
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        //判断条件值是否为空，不为空拼接
        if (!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())) { //一级分类
            wrapper.eq("subject_parent_id", courseFrontVo.getSubjectParentId());
        }
        if (!StringUtils.isEmpty(courseFrontVo.getSubjectId())) { //二级分类
            wrapper.eq("subject_id", courseFrontVo.getSubjectId());
        }
        if (!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())) { //关注度
            wrapper.orderByDesc("buy_count");
        }
        if (!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())) { //最新
            wrapper.orderByDesc("gmt_create");
        }

        if (!StringUtils.isEmpty(courseFrontVo.getPriceSort())) {//价格
            wrapper.orderByDesc("price");
        }

        baseMapper.selectPage(pageParam, wrapper);

        List<EduCourse> records = pageParam.getRecords();
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();//下一页
        boolean hasPrevious = pageParam.hasPrevious();//上一页

        //把分页数据获取出来，放到map集合
        Map<String, Object> map = new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        //map返回
        return map;

    }

    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {


        return baseMapper.getBaseCourseInfo(courseId);
    }
}
